<?php

namespace app\bews\controller;

use app\bews\model\Base;
use think\app;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Article extends Base
{
    public function articleInfo(){
        $lists = Db::table("bew_article")->select();
        View::assign([
            'lists'=>$lists,
        ]);
        return View::fetch();

    }

    public function articleAdd(){
        if(Request::isPost()){
//            print_r(input('post.'));
            $title = input('post.title');
            $keywords = input('post.keywords');
            $desc = input('post.desc');
            $author = input('post.author');
            $thumb = input('post.thumb');
            $content = input('post.content');
            $click = input('post.click');
            $zan = input('post.zan');
            $rec = input('post.rec');
            $time = input('post.time');
            $cateid = input('post.cateid');
            $date = input('post.date');
            $EC = input('post.EC');
            $contenten = input('post.contenten');
            $admin = input('post.admin');
            $titleen = input('post.titleen');
            $suspendtime = input('post.suspendtime');

            $insert = Db::table('bew_article')->insert([
                'title'=>$title,
                'keywords'=>$keywords,
                'desc'=>$desc,
                'author'=>$author,
                'thumb'=>$thumb,
                'content'=>$content,
                'click'=>$click,
                'zan'=>$zan,
                'rec'=>$rec,
                'time'=>$time,
                'cateid'=>$cateid,
                'date'=>$date,
                'EC'=>$EC,
                'contenten'=>$contenten,
                'admin'=>$admin,
                'titleen'=>$titleen,
                'suspendtime'=>$suspendtime
            ]);
            if(empty($insert)){
                echo json_encode(['code'=>1,'msg'=>'添加失败,请重试']);
                exit();
            }else{
                echo json_encode(['code'=>0,'msg'=>'添加成功']);
            }
        }else{
            return View::fetch();
        }
    }

    public function articleModify(){
        if(Request::isPost()){
            $id = input('post.id');
            $title = input('post.title');
            $keywords = input('post.keywords');
            $desc = input('post.desc');
            $author = input('post.author');
            $thumb = input('post.thumb');
            $content = input('post.content');
            $click = input('post.click');
            $zan = input('post.zan');
            $rec = input('post.rec');
            $time = input('post.time');
            $cateid = input('post.cateid');
            $date = input('post.date');
            $EC = input('post.EC');
            $contenten = input('post.contenten');
            $admin = input('post.admin');
            $titleen = input('post.titleen');
            $suspendtime = input('post.suspendtime');

            $update = Db::table("bew_article")->where('id',$id)->update([
                'title'=>$title,
                'keywords'=>$keywords,
                'desc'=>$desc,
                'author'=>$author,
                'thumb'=>$thumb,
                'content'=>$content,
                'click'=>$click,
                'zan'=>$zan,
                'rec'=>$rec,
                'time'=>$time,
                'cateid'=>$cateid,
                'date'=>$date,
                'EC'=>$EC,
                'contenten'=>$contenten,
                'admin'=>$admin,
                'titleen'=>$titleen,
                'suspendtime'=>$suspendtime
            ]);
            if($update == null){
                echo json_encode(['code'=>1,'msg'=>'修改失败,请重试']);
                exit();
            }
            echo json_encode(['code'=>0,'msg'=>'修改成功']);
        }else{
            $id = input('get.id');
            if(empty($id)){
                $this->error("请传入id");
                exit();
            }
            $find = Db::table("bew_article")->where('id',$id)->find();
            View::assign([
                'find'=>$find
            ]);
//            print_r($find);
            return View::fetch();
        }
    }

    public function articleDel(){
        $id = input('post.id');
        if(empty($id)){
            $this->error("请传入id");
            exit();
        }
        $del = Db::table("bew_article")->where('id',$id)->delete();
        if($del == null){
            echo json_encode(['code'=>1,'msg'=>'删除失败,请重试']);
            exit();
        }
        echo json_encode(['code'=>0,'msg'=>'删除成功']);
        return View::fetch();
    }
}